home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue33 / random / ezdslthd.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-03-18  |  8.2 KB  |  312 lines

  1. {===EZDSLTHD==========================================================
  2.  
  3. Part of the EZ Delphi Structures Library--the thread support routines
  4.  
  5. EZDSLTHD is Copyright (c) 1995-1998 by Julian M. Bucknall
  6.  
  7. VERSION HISTORY
  8. 18Mar98 JMB 3.00 Initial release (BETA TEST)
  9. ======================================================================}
  10. { Copyright (c) 1993-1998, Julian M. Bucknall. All Rights Reserved   }
  11.  
  12. unit EZDSLThd;
  13.  
  14. {$I EZDSLDEF.INC}
  15. {---Place any compiler options you require here-----------------------}
  16.  
  17.  
  18. {---------------------------------------------------------------------}
  19. {$I EZDSLOPT.INC}
  20.  
  21. {$IFNDEF Win32}
  22.   !! This unit is for 32-bit programming only
  23. {$ENDIF}
  24.  
  25. interface
  26.  
  27. uses
  28.   {$IFDEF Win32}
  29.   Windows,
  30.   {$ELSE}
  31.   WinTypes,
  32.   WinProcs,
  33.   {$ENDIF}
  34.   SysUtils;
  35.  
  36. type
  37.   TezWaitResult = (                 {"Wait for" results...}
  38.                    arSuccess,       {..successful}
  39.                    arTimeout,       {..wait timed out}
  40.                    arAbandoned,     {..object was abandoned}
  41.                    arFailed);       {..call failed}
  42.  
  43.   TezEventResetType = (             {Types of Win32 Events...}
  44.                        ertAuto,     {..autoreset event}
  45.                        ertManual);  {..manual reset event}
  46.  
  47. type
  48.   TezResourceLock = class
  49.     {-Encapsulation of Win32's Critical Section}
  50.     protected {private}
  51.       rlCritSect : TRTLCriticalSection;
  52.     protected
  53.     public
  54.       constructor Create;
  55.       destructor Destroy; override;
  56.  
  57.       procedure Lock;
  58.       procedure Unlock;
  59.   end;
  60.  
  61.   TezWaitableObject = class
  62.     {-A Win32 Kernel synchronization object that can be waited on to
  63.       be signalled and that can be released}
  64.     protected {private}
  65.       woLastError : integer;
  66.     protected
  67.       woHandle    : THandle;
  68.  
  69.       procedure woResetError;
  70.       procedure woRetrieveError;
  71.     public
  72.       destructor Destroy; override;
  73.  
  74.       function IsValid : boolean;
  75.       function Release  : boolean; virtual;
  76.       function WaitFor(aTimeOut : integer) : TezWaitResult;
  77.  
  78.       property Handle : THandle read woHandle;
  79.       property LastError : integer read woLastError;
  80.     end;
  81.  
  82.   TezMutex = class (TezWaitableObject)
  83.     {-A Win32 Mutex}
  84.     protected {private}
  85.     protected
  86.     public
  87.       constructor Create(const aMutexName    : string;
  88.                                aOpenExisting : boolean);
  89.       function Release  : boolean; override;
  90.   end;
  91.  
  92.   TezEvent = class (TezWaitableObject)
  93.     {-A Win32 Event}
  94.     protected {private}
  95.     protected
  96.     public
  97.       constructor Create(const aEventName    : string;
  98.                                aOpenExisting : boolean;
  99.                                aResetType    : TezEventResetType;
  100.                                aInitSignaled : boolean);
  101.       function Pulse : boolean;
  102.       function Reset : boolean;
  103.       function Signal : boolean;
  104.   end;
  105.  
  106.   TezSemaphore = class (TezWaitableObject)
  107.     {-A Win32 Semaphore}
  108.     protected {private}
  109.     protected
  110.     public
  111.       constructor Create(const aSemaphoreName : string;
  112.                                aOpenExisting  : boolean;
  113.                                aInitCount     : integer;
  114.                                aMaxCount      : integer);
  115.       function Release  : boolean; override;
  116.   end;
  117.  
  118.  
  119. implementation
  120.  
  121. const
  122.   SEMAPHORE_MODIFY_STATE = $0002; {missed out from Delphi's WINDOWS.PAS}
  123.  
  124.  
  125. {===TezResourceLock==================================================}
  126. constructor TezResourceLock.Create;
  127. begin
  128.   inherited Create;
  129.   InitializeCriticalSection(rlCritSect);
  130. end;
  131. {--------}
  132. destructor TezResourceLock.Destroy;
  133. begin
  134.   DeleteCriticalSection(rlCritSect);
  135.   inherited Destroy;
  136. end;
  137. {--------}
  138. procedure TezResourceLock.Lock;
  139. begin
  140.   EnterCriticalSection(rlCritSect);
  141. end;
  142. {--------}
  143. procedure TezResourceLock.Unlock;
  144. begin
  145.   LeaveCriticalSection(rlCritSect);
  146. end;
  147. {====================================================================}
  148.  
  149.  
  150. {===TezWaitableObject================================================}
  151. destructor TezWaitableObject.Destroy;
  152. begin
  153.   if (woHandle <> INVALID_HANDLE_VALUE) then
  154.     CloseHandle(woHandle);
  155.   inherited Destroy;
  156. end;
  157. {--------}
  158. function TezWaitableObject.IsValid : boolean;
  159. begin
  160.   if (Handle <> INVALID_HANDLE_VALUE) then begin
  161.     Result := true;
  162.     woLastError := 0;
  163.   end
  164.   else begin
  165.     Result := false;
  166.     woLastError := INVALID_HANDLE_VALUE;
  167.   end;
  168. end;
  169. {--------}
  170. function TezWaitableObject.Release  : boolean;
  171. begin
  172.   Result := true;
  173. end;
  174. {--------}
  175. function TezWaitableObject.WaitFor(aTimeOut : integer) : TezWaitResult;
  176. var
  177.   WaitResult : integer;
  178. begin
  179.   woResetError;
  180.   WaitResult := WaitForSingleObject(woHandle, aTimeOut);
  181.   if (WaitResult = WAIT_OBJECT_0) then
  182.     Result := arSuccess
  183.   else if (WaitResult = WAIT_TIMEOUT) then
  184.     Result := arTimeout
  185.   else if (WaitResult = WAIT_ABANDONED) then
  186.     Result := arAbandoned
  187.   else begin
  188.     Result := arFailed;
  189.     woRetrieveError;
  190.   end;
  191. end;
  192. {--------}
  193. procedure TezWaitableObject.woResetError;
  194. begin
  195.   woLastError := 0;
  196. end;
  197. {--------}
  198. procedure TezWaitableObject.woRetrieveError;
  199. begin
  200.   woLastError := Windows.GetLastError;
  201. end;
  202. {====================================================================}
  203.  
  204.  
  205. {===TezMutex=========================================================}
  206. constructor TezMutex.Create(const aMutexName    : string;
  207.                                   aOpenExisting : boolean);
  208. begin
  209.   inherited Create;
  210.   if aOpenExisting then
  211.     woHandle := OpenMutex(MUTEX_ALL_ACCESS, false, PChar(aMutexName))
  212.   else
  213.     woHandle := CreateMutex(nil, false, PChar(aMutexName));
  214.   if (woHandle = 0) then begin
  215.     woHandle := INVALID_HANDLE_VALUE;
  216.     woRetrieveError;
  217.   end;
  218. end;
  219. {--------}
  220. function TezMutex.Release : boolean;
  221. begin
  222.   Result := false;
  223.   if IsValid then
  224.     if ReleaseMutex(Handle) then
  225.       Result := true
  226.     else
  227.       woRetrieveError;
  228. end;
  229. {====================================================================}
  230.  
  231.  
  232. {===TezEvent=========================================================}
  233. constructor TezEvent.Create(const aEventName    : string;
  234.                                   aOpenExisting : boolean;
  235.                                   aResetType    : TezEventResetType;
  236.                                   aInitSignaled : boolean);
  237. begin
  238.   inherited Create;
  239.   if aOpenExisting then
  240.     woHandle := OpenEvent(EVENT_MODIFY_STATE, false, PChar(aEventName))
  241.   else
  242.     woHandle := CreateEvent(nil, (aResetType = ertManual),
  243.                             aInitSignaled, PChar(aEventName));
  244.   if (woHandle = 0) then begin
  245.     woHandle := INVALID_HANDLE_VALUE;
  246.     woRetrieveError;
  247.   end;
  248. end;
  249. {--------}
  250. function TezEvent.Pulse : boolean;
  251. begin
  252.   Result := false;
  253.   if IsValid then
  254.     if PulseEvent(Handle) then
  255.       Result := true
  256.     else
  257.       woRetrieveError;
  258. end;
  259. {--------}
  260. function TezEvent.Reset : boolean;
  261. begin
  262.   Result := false;
  263.   if IsValid then
  264.     if ResetEvent(Handle) then
  265.       Result := true
  266.     else
  267.       woRetrieveError;
  268. end;
  269. {--------}
  270. function TezEvent.Signal : boolean;
  271. begin
  272.   Result := false;
  273.   if IsValid then
  274.     if SetEvent(Handle) then
  275.       Result := true
  276.     else
  277.       woRetrieveError;
  278. end;
  279. {====================================================================}
  280.  
  281.  
  282. {===TezSemaphore=====================================================}
  283. constructor TezSemaphore.Create(const aSemaphoreName : string;
  284.                                       aOpenExisting  : boolean;
  285.                                       aInitCount     : integer;
  286.                                       aMaxCount      : integer);
  287. begin
  288.   inherited Create;
  289.   if aOpenExisting then
  290.     woHandle := OpenSemaphore(SEMAPHORE_MODIFY_STATE, false, PChar(aSemaphoreName))
  291.   else
  292.     woHandle := CreateSemaphore(nil, aInitCount, aMaxCount, PChar(aSemaphoreName));
  293.   if (woHandle = 0) then begin
  294.     woHandle := INVALID_HANDLE_VALUE;
  295.     woRetrieveError;
  296.   end;
  297. end;
  298. {--------}
  299. function TezSemaphore.Release  : boolean;
  300. begin
  301.   Result := false;
  302.   if IsValid then
  303.     if ReleaseSemaphore(Handle, 1, nil) then
  304.       Result := true
  305.     else
  306.       woRetrieveError;
  307. end;
  308. {====================================================================}
  309.  
  310.  
  311. end.
  312.